package cn.tedu.egmvc3.controller;

import cn.tedu.egmvc3.mapper.UserMapper;
import cn.tedu.egmvc3.pojo.dto.UserInsertParam;
import cn.tedu.egmvc3.pojo.dto.UserUpdateParam;
import cn.tedu.egmvc3.pojo.entity.User;
import cn.tedu.egmvc3.pojo.vo.UserListVO;
import org.springframework.beans.BeanUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;

import java.util.Date;
import java.util.List;

@RestController
@RequestMapping("/v1/user/")
public class UserController {
    @Autowired
    private UserMapper userMapper;
    //添加用户的功能
    @PostMapping("add")
    public String insert(UserInsertParam userInsertParam){
        System.out.println("输出前端传过来的值"+ userInsertParam);
        User user = new User();
        //复制参数
        BeanUtils.copyProperties(userInsertParam,user);
        user.setCreateTime(new Date());
        user.setUpdateTime(new Date());
        int i =userMapper.insert(user);
        return "添加了"+i+"条";
    }
    //查询用户的功能
    @GetMapping("list")
    public void select(){
        List<UserListVO> select = userMapper.select();
        for (UserListVO userListVO :select){
            System.out.println(userListVO);
        }
    }
    //添加删除功能
    @PostMapping("del")
    public String delete(Integer id){
        int i =userMapper.delete(id);
        return "删除了"+i+"条";
    }
    //添加了修改的功能
    @PostMapping("update")
    public User update(UserUpdateParam userUpdateParam){
        System.out.println("传入的参数是"+userUpdateParam);
        User user = new User();
        //赋值
        BeanUtils.copyProperties(userUpdateParam,user);
        userMapper.update(user);
        return user;
    }
}
